package impexp.importfile;

import java.util.List;

import org.json.JSONArray;
import org.json.JSONObject;

import com.aote.util.XMLReaderUtil;

public class HibernateConfigAnaly implements IDataAnaly {

	private String configpath;
	
	@Override
	public JSONArray analy(List<String[]> data) {
		
		JSONArray result = new JSONArray();
		
		XMLReaderUtil read = new XMLReaderUtil();
		Object[] field = read.getColumn(configpath, data.get(0));
		for (int i = 1; i < data.size(); i++) {
			JSONObject temp = new JSONObject();

			// 若有子json对象，保存在子json中增加的excel下角标、
			int dataIndex = 0;
			for (int j = 0; j < field.length; j++) {
				if (field[j] instanceof JSONObject) {
					JSONObject children = (JSONObject) field[j];
					// 一般只有一个
					for (String s : children.keySet()) {
						JSONObject childrenjson = new JSONObject();
						if (children.get(s) instanceof Object[]) {
							Object[] obj = (Object[]) children.get(s);
							for (int z = 0; z < obj.length; z++) {
								childrenjson.put(obj[z].toString(),
										data.get(i)[j + dataIndex]);
								if (z != obj.length - 1) {
									dataIndex++;
								}
							}
						}
						temp.put(s, childrenjson);
					}
				} else {

					temp.put(field[j].toString(), data.get(i)[j + dataIndex]);
				}

			}
			result.put(temp);
		}
		return result;
	}
	
	public String getConfigpath() {
		return configpath;
	}

	public void setConfigpath(String configpath) {
		this.configpath = configpath;
	}

}
